package com.zimmor.mq.rocket_train.order;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2023/2/14 9:36
 * @Description 消费顺序消息
 * @version: 1.0.0
 */

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建PushConsumer
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group2");
        //设置namesrv
        consumer.setNamesrvAddr("192.168.204.136:9876;192.168.204.137:9876");
        //设置消息模式
        consumer.setMessageModel(MessageModel.CLUSTERING);
        //订阅主题设置tag
        consumer.subscribe("order", "TagA");
        //注册监听器
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("线程名称：" + Thread.currentThread().getName() + "消息内容：" + new String(messageExt.getBody())+" keys:" + messageExt.getKeys() + " tag:" + messageExt.getTags());
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //启动consumer
        consumer.start();
    }
}
